package com.sherry.base;

import com.google.protobuf.GeneratedMessage;
import com.sherry.uitils.UrlBuilder;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import javax.swing.text.html.HTMLDocument.Iterator;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by swl on 2021/6/21.
 */
public class TestBase {

    public static ByteArrayOutputStream doJsonPost(GeneratedMessage message, String url, HashMap<String, String> headermap) {
        URI uri = UrlBuilder.geturl(url.trim());
        System.out.println(uri.toString());

        HttpPost post = new HttpPost(uri);
        for (String key : headermap.keySet()) {
            post.addHeader(key, headermap.get(key));
        }

        ByteArrayInputStream inputStream = new ByteArrayInputStream(message.toByteArray());
        InputStreamEntity inputStreamEntity = new InputStreamEntity(inputStream);
        post.setEntity(inputStreamEntity);
        return postInfo(post);
    }

    public static ByteArrayOutputStream postInfo(HttpPost post){

        CloseableHttpClient httpClient = HttpClients.createDefault();
        ByteArrayOutputStream outputStream = null;
        try {
            CloseableHttpResponse response = httpClient.execute(post);
            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();
                outputStream = new ByteArrayOutputStream();
                entity.writeTo(outputStream);
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return outputStream;
    }

    public static ByteArrayOutputStream doJsonPost(String url, HashMap<String, String> headermap) {
        URI uri = UrlBuilder.geturl(url.trim());
        HttpPost post = new HttpPost(uri);
        for (String key : headermap.keySet()) {
            post.addHeader(key, headermap.get(key));
        }
        System.out.println(uri);
        return postInfo(post);
    }

    public static ByteArrayOutputStream doJsonPost(String url) {
        URI uri = UrlBuilder.geturl(url.trim());
        HttpPost post = new HttpPost(uri);
        System.out.println(uri);
        return postInfo(post);
    }

    public static ByteArrayOutputStream doJsonPost(String url, HashMap<String, String> headermap, final Map<String,String> paramMap) {
        StringBuilder postParam = new StringBuilder();
        for (int i =0; i< paramMap.keySet().size();i++) {
            if (i>0){
                postParam.append("&");
            }
            postParam.append(paramMap.keySet()).append("=").append(paramMap.get(i));
        }

        url = url + "?" + postParam;
        URI uri = UrlBuilder.geturl(url.trim());
        HttpPost post = new HttpPost(uri);
        for (String key : headermap.keySet()) {
            post.addHeader(key, headermap.get(key));
        }
        System.out.println(uri);
        return postInfo(post);
    }
}
